package com.tyut.homework.controller;

import com.tyut.homework.dto.ClazzPeopleNum;
import com.tyut.homework.dto.DormitoryDTO;
import com.tyut.homework.pojo.Dormitory;
import com.tyut.homework.pojo.Dormitory;
import com.tyut.homework.response.ServerResponse;
import com.tyut.homework.service.DormitoryService;
import com.tyut.homework.service.FinanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("宿舍管理")
@RestController
@RequestMapping("/dormitory")
public class DormitoryController extends ControllerTemplate<Dormitory> {

    @Autowired
    private DormitoryService dormitoryService;

    @ApiOperation("查询单个")
    @GetMapping("/findById/{id}")
    public ServerResponse findById(@PathVariable Integer id) {
        return ServerResponse.createBySuccess(dormitoryService.find(id));
    }

    @ApiOperation("查看分页信息")
    @GetMapping("/findAll/{pageNum}/{pageLimit}")
    public ServerResponse findAll(@PathVariable Integer pageNum,
                                  @PathVariable Integer pageLimit) {
        return ServerResponse.createBySuccess(dormitoryService.getPage(pageNum, pageLimit));
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public ServerResponse add(Dormitory dormitory) {
        return ServerResponse.createBySuccess(dormitoryService.save(dormitory));
    }

    @ApiOperation("更新信息")
    @PostMapping("/update")
    public ServerResponse update(Dormitory dormitory) {
        return ServerResponse.createBySuccess(dormitoryService.update(dormitory));
    }

    @ApiOperation("删除单个")
    @GetMapping("/delete/{id}")
    public ServerResponse delete(@PathVariable Integer id) {
        return ServerResponse.createBySuccess("delete success", dormitoryService.delete(id));
    }

    @ApiOperation("分配宿舍")
    @GetMapping("/getDors/{apartsNum}")
    public ServerResponse getDors(@PathVariable Integer apartsNum) {
        dormitoryService.getDors(apartsNum);
        return ServerResponse.createBySuccess("添加成功!");
    }

    @ApiOperation("给status=0的男生自动分配宿舍")
    @GetMapping("/getBoyNotPatch")
    ServerResponse autoMatchDorForMan() {
        List<ClazzPeopleNum> result = dormitoryService.getAllClazzPeopleNum("男");
        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation("给status=0的女生自动分配宿舍")
    @GetMapping("/getGirlNotPatch")
    ServerResponse autoMatchDorForWomen() {
        List<ClazzPeopleNum> result = dormitoryService.getAllClazzPeopleNum("女");
        return ServerResponse.createBySuccess(result);
    }

    @ApiOperation("自动分配宿舍")
    @GetMapping("/autoMatch")
    public ServerResponse autoMatch() {
        dormitoryService.autoMatch();
        return ServerResponse.createBySuccess();
    }

    @ApiOperation("获取宿舍信息")
    @GetMapping("/getListInfo")
    public ServerResponse getDOrsStatus() {
        return ServerResponse.createBySuccess(dormitoryService.getDOrsStatus());
    }

    @ApiOperation("/根据班级查看宿舍列表")
    @GetMapping("/getListByClazz/{clazz}")
    public ServerResponse getListByClazz(@PathVariable String clazz){
        List<DormitoryDTO> byClazz = dormitoryService.findByClazz(clazz);
        return ServerResponse.createBySuccess(byClazz);
    }

    @ApiOperation("添加学生到宿舍")
    @PostMapping("/addStuInDor")
    public ServerResponse addStuInDor(@RequestParam Integer stuId,
                                      @RequestParam Integer aptId,
                                      @RequestParam Integer dorId){
        dormitoryService.insertInDor(stuId, aptId, dorId);
        return ServerResponse.createBySuccess();
    }
}
